package com.example.dense_rack_server.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.dense_rack_server.entity.ArchivesType;
import com.example.dense_rack_server.entityVo.MenuALLVo;
import com.example.dense_rack_server.entityVo.MyAreaTreeVo;
import com.example.dense_rack_server.mapper.ArchivesTypeMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dense_rack_server.utils.DateFormat;
import com.example.dense_rack_server.utils.ResponseModel;
import com.example.dense_rack_server.utils.TreeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 档案分类 服务实现类
 * </p>
 *
 * @author  张彬 
 * @since 2025-03-03
 */
@Service
public class ArchivesTypeService extends ServiceImpl<ArchivesTypeMapper, ArchivesType> {

    @Resource
    public ArchivesTypeMapper archivesTypeMapper;

    /**
     * 绘制分类树
     */
    public Map<String, Object> buildTypeTree() {
        List<MyAreaTreeVo> myDeptTreeVoList = archivesTypeMapper.buildType();
        List<MyAreaTreeVo> resultList = TreeUtil.buildAreaTreeAll(myDeptTreeVoList);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("message", 200);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("status", map);
        resultMap.put("data", resultList);
        return resultMap;
    }

    /**
     * 档案分类列表
     */
    public Map<String, Object> typeList() {
        QueryWrapper<ArchivesType> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        List<ArchivesType> list = archivesTypeMapper.selectList(queryWrapper);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("msg", "请求成功");
        resultMap.put("data", list);
        resultMap.put("count", 1000);
        return resultMap;
    }


    /**
     * 绘制分类树
     */
    public ResponseModel buildTypeTreeTable() {
        List<MenuALLVo> sysUserVoList = archivesTypeMapper.buildTypeTree();
        return ResponseModel.success(sysUserVoList);
    }

    /**
     * 新增分类保存
     */
    public ResponseModel saveType(JSONObject data) {
        QueryWrapper<ArchivesType> deptQueryWrapper = new QueryWrapper<>();
        deptQueryWrapper.eq("type_name", data.getStr("typeName"));
        ArchivesType archivesType = archivesTypeMapper.selectOne(deptQueryWrapper);
        if (archivesType != null) {
            return ResponseModel.fail("分类名称重复");
        }
        ArchivesType insertType = new ArchivesType();
        if (StrUtil.isEmpty(data.getStr("parentId"))) {
            insertType.setParentId(0);
        } else {
            insertType.setParentId(data.getInt("parentId"));
        }
        insertType.setTypeName(data.getStr("typeName"));
        insertType.setSort(data.getInt("sort"));
        insertType.setStatus(data.getInt("status"));
        insertType.setCreateTime(DateFormat.getTime());
        insertType.setUpdateTime(DateFormat.getTime());
        int insertStatus = archivesTypeMapper.insert(insertType);
        return insertStatus > 0 ? ResponseModel.success("新增  " + data.getStr("typeName") + "  分类成功") : ResponseModel.fail("新增分类失败");
    }

    /**
     * 修改分类保存
     */
    public ResponseModel updateType(JSONObject data) {
        ArchivesType updateType = new ArchivesType();
        updateType.setId(data.getInt("id"));
        if (StrUtil.isEmpty(data.getStr("parentId"))) {
            updateType.setParentId(0);
        } else {
            updateType.setParentId(data.getInt("parentId"));
        }
        updateType.setTypeName(data.getStr("typeName"));
        updateType.setSort(data.getInt("sort"));
        updateType.setStatus(data.getInt("status"));
        updateType.setUpdateTime(DateFormat.getTime());
        int updateStatus = archivesTypeMapper.updateById(updateType);
        return updateStatus > 0 ? ResponseModel.success("修改  " + data.getStr("typeName") + "  分类成功") : ResponseModel.fail("修改分类失败");
    }

    /**
     * 档案区域状态修改
     */
    public ResponseModel updateTypeStatus(JSONObject data) {
        UpdateWrapper<ArchivesType> myUserUpdateWrapper = new UpdateWrapper<>();
        //条件构造器Set方法
        //假设只更新一个字段在使用updateWrapper 的构造器中也需要构造一个实体对象,这样比较麻烦。可以使用updateWrapper的set方法
        myUserUpdateWrapper.eq("id", data.getInt("id")).set("status", data.getInt("status"));
        int count = archivesTypeMapper.update(null, myUserUpdateWrapper);
        if (count > 0) {
            if (data.getInt("status") == 0) {
                return ResponseModel.success("禁用成功");
            } else {
                return ResponseModel.success("启用成功");
            }
        } else {
            return ResponseModel.fail("请求失败");
        }
    }

    /**
     * 删除功能档案
     */
    public ResponseModel deleteType(String id) {
        String[] strings = id.split(",");
        List<String> idList = CollUtil.newArrayList(strings);
        archivesTypeMapper.deleteBatchIds(idList);
        return ResponseModel.success("删除分类成功");
    }

}
